package com.codeduck.mybatis.service;

import com.codeduck.mybatis.entity.DictDO;
import com.codeduck.mybatis.entity.DictQuery;
import com.codeduck.mybatis.mapper.DictMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: <a href="mailto:codeduck@163.com">Codeduck</a>
 * @date: 2022-09-01
 */
@Service
@Slf4j
public class DictService {

    @Resource
    private DictMapper dictMapper;

    public boolean addDict(String dictKey, String dictName, String dickVal) throws Exception {
        if (StringUtils.isBlank(dictKey) || StringUtils.isBlank(dictName) || StringUtils.isBlank(dickVal)) {
            throw new Exception("参数不能为空");
        }
        DictDO dictDO = DictDO.builder().dictKey(dictKey).dictName(dictName).dictValue(dickVal)
                .dictOrder("999").dictType("private").build();
        int i = dictMapper.saveDict(dictDO);
        if (i <= 0) {
            throw new Exception("新增字典失败");
        }
        return i > 0;
    }

    public List<DictDO> getListByKey(String dictKey) {
        if (StringUtils.isBlank(dictKey)) {
            return null;
        }
        DictQuery dictQuery = new DictQuery();
        dictQuery.setDictKey(dictKey);
        return dictMapper.queryDictList(dictQuery);
    }

    public DictDO getById(Integer dictId) throws Exception {
        if (null == dictId) {
            throw new Exception("参数不能为空");
        }
        DictDO dict = dictMapper.getByDictId(dictId);
        log.warn("DictService#getById 被调用，查询到的对象为：[{}]", dict);
        return dict;
    }
}
